import request from '@/utils/axios'
import { strToNumberArr } from '@/utils'

export interface RoleType {
  id: number | undefined
  name: string
  code: string
  sort: number
  status: number
  type: number
  dataScope: number | undefined
  dataScopeDepartIds: number[]
  remark: string
}

export interface RolePageReqType extends PageParam {
  name?: string
  code?: string
  status?: number
  create_time?: Date[]
}

export interface RolePageType {
  list: RoleType[]
  total: number
}

export interface UpdateStatusReqType {
  id: number
  status: number
}

/**
 * 角色接口
 */
export const RoleApi = {
  // 新增
  create: (data: RoleType) => {
    return request.post({ url: '/system/role/create', data, is_mock: true })
  },
  // 修改
  update: (data: RoleType) => {
    return request.put({ url: '/system/role/update', data, is_mock: true })
  },
  // 状态修改
  updateStatus: (id: number, status: number) => {
    const data = {
      id,
      status
    }
    return request.put({ url: '/system/role/update-status', data, is_mock: true })
  },
  // 删除
  delete: (id: number) => {
    return request.delete({ url: '/system/role/delete?id=' + id, is_mock: true })
  },
  // 查询详情
  detail: (id: number) => {
    return request.get({ url: '/system/role/detail?id=' + id, is_mock: true })
  },
  // 查询分页列表
  page: async (params: RolePageReqType): Promise<RolePageType> => {
    const result: any = await request.get({ url: '/system/role/page', params, is_mock: true })
    result.list.forEach(item => {
      item.dataScopeDepartIds = strToNumberArr(item.dataScopeDepartIds)
    })
    return result
  },
  // 查询精简列表
  simple: (): Promise<RoleType[]> => {
    return request.get({ url: '/system/role/simple-list', is_mock: true })
  },
  // 导出
  export: (params: RolePageReqType) => {
    return request.download({ url: '/system/role/export', params, is_mock: true })
  },
  // 获取当前登录用户可看到的角色
  getRoleByUser: async () => {
    return await request.post({ url: '/Api/getuserrole' })
  }
}
